<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ITStock extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
    	if (!isset($_SESSION['username']) || $_SESSION['username'] != 'dara'){
	    	redirect('administration','refresh');
	    }
        $this->load->model('MITStock');
        $this->load->model('MStatic');
        $this->load->model('MSetting');
	    $this->load->helper('csv');
    }
    
    public function index()
    {
        $data['title'] = "IT Inventory";
		$data['main'] = 'itstock/inventorylist';
        $data['leftside'] = 'content-left';     
        $data['lefttype'] = 'itstock';
        $data['items'] = $this->MITStock->getStockList();
		$this->load->vars($data); 
		$this->load->view('itstock/layout');       
    }
    
    public function searchStock()
    {
        if($this->input->post('search') || $this->input->post('datepurchased'))
        {
            $data['title'] = "IT Inventory";
    		$data['main'] = 'itstock/inventorylist';
            $data['leftside'] = 'content-left';     
            $data['lefttype'] = 'itstock';
            $data['items'] = $this->MITStock->getStockList($this->input->post('search'), $this->input->post('datepurchased'));
    		$this->load->vars($data);
    		$this->load->view('itstock/layout');    
        }else{
            redirect('itstock','refresh');
        }
    }
    
    public function stockAdd()
    {
       if($this->input->post('item'))
       {
            $this->MITStock->addEditStock();
	  		$this->session->set_flashdata('message','Stock added');
	  		redirect('itstock','refresh');
       }
		$data['title'] = "Add New Item";
		$data['main'] = 'itstock/stockadd';
        $data['leftside'] = 'content-left';     
        $data['lefttype'] = 'itstock';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('itstock');
        $data['itTypeList'] = $this->MStatic->getITTypeDropdown();
        $data['modelList'] = $this->MStatic->getModelDropdown();
        $data['statusList'] = $this->MStatic->getStatusDropdown();
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
		$this->load->vars($data);
		$this->load->view('itstock/layout'); 
    }
    
    public function stockEdit($id=0)
    {
       if($this->input->post('item'))
       {
            $this->MITStock->addEditStock(id_clean($this->input->post('stock_id')));
	  		$this->session->set_flashdata('message','Stock updated');
	  		redirect('itstock','refresh');
       }
		$data['title'] = "Update item in Stock Inventory";
		$data['main'] = 'itstock/stockedit';
        $data['leftside'] = 'content-left';     
        $data['lefttype'] = 'itstock';
        $data['stock'] = $this->MITStock->getStockById($id);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('itstock','itstock/deletestock/'.$id);
        $data['itTypeList'] = $this->MStatic->getITTypeDropdown();
        $data['modelList'] = $this->MStatic->getModelDropdown();
        $data['statusList'] = $this->MStatic->getStatusDropdown();
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
		$this->load->vars($data);
		$this->load->view('itstock/layout'); 
    }   
    
    public function Used()
    {
        $data['title'] = "List Used and Transfered Items in Stock Inventory System";
		$data['main'] = 'itstock/usedlist';
        $data['leftside'] = 'content-left';     
        $data['lefttype'] = 'itstock';
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['ittypeList'] = $this->MStatic->getITTypeDropdown();
        $data['modelList'] = $this->MStatic->getModelDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
        
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $ittype = $this->input->post('ittype');
        $model = $this->input->post('model');
        $branch = $this->input->post('branch');
        $location = $this->input->post('location');
        $submit = $this->input->post('submit');
        if($submit)
        {
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
            $data['ittype'] = $ittype;
            $data['model'] = $model;
            $data['location'] = $location; 
            $data['items'] = $this->MITStock->getItemsUsed($start, $end, $branch, false,$ittype,$model,$location)->result();
        }else{
            $data['items'] = $this->MITStock->getItemsUsed()->result();
        }
		$this->load->vars($data);
		$this->load->view('itstock/layout'); 
    } 
    
    public function exportItem()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $ittype = $this->input->post('ittype');
        $model = $this->input->post('model');
        $branch = $this->input->post('branch');
        $location = $this->input->post('location');
        if(!empty($branch))
        {
            $data['xlbranch'] = $branch;   
        }else{
            $data['xlbranch'] = $_SESSION['branch'];
        }
        $data['title'] = "List Used and Transfered Items in Stock Inventory System";
        $data['xlname'] = "ItemList";
        $data['items'] = $this->MITStock->getItemsUsed($start, $end, $branch, false,$ittype,$model,$location)->result();
        $this->load->vars($data);
		$this->load->view('itstock/usedlist_exp'); 
        //query_to_csv($items,TRUE,'Inventory_Used_'.date('dMy').'.csv');
    }
    
    public function broken()
    {
        $data['title'] = "List Broken Items in Stock Inventory System";
		$data['main'] = 'itstock/brokenlist';
        $data['leftside'] = 'content-left';     
        $data['lefttype'] = 'itstock';
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        if($start || $end)
        {
            $data['datestart'] = $start;
            $data['dateend'] = $end; 
            $data['items'] = $this->MITStock->getItemsBroken($start,$end)->result();
    		
        }else{
            $data['items'] = $this->MITStock->getItemsBroken()->result();
        }
        $this->load->vars($data);
    	$this->load->view('itstock/layout'); 
    }
    
    public function exportBroken()
    {
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $data['items'] = $this->MITStock->getItemsBroken($start,$end)->result();
        if(!empty($branch))
        {
            $data['xlbranch'] = $branch;   
        }else{
            $data['xlbranch'] = $_SESSION['branch'];
        }
        $data['title'] = "List Broken Items in Stock Inventory System";
        $data['xlname'] = "ItemBroken";
        $this->load->vars($data);
		$this->load->view('itstock/brokenlist_exp'); 
        //$items = $this->MITStock->getItemsBroken($start, $end, true);
        //query_to_csv($items,TRUE,'Inventory_Broken_'.date('dMy').'.csv');
    }
    
    public function usedEdit($id=0, $eleId=0)
    {
       if($this->input->post('unitused'))
       {
            $this->MITStock->addEditItem(id_clean($this->input->post('stock_id')), id_clean($this->input->post('item_id')));
	  		$this->session->set_flashdata('message','Item updated');
	  		redirect('itstock/used','refresh');
       }
		$data['title'] = "Update item in Stock Inventory";
		$data['main'] = 'itstock/usededit';
        $data['leftside'] = 'content-left';     
        $data['lefttype'] = 'itstock';
        $data['stock'] = $this->MITStock->getItemByStockIdItemId($id, $eleId);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('itstock/used','itstock/deleteused/'.$eleId);
        $data['statusList'] = $this->MStatic->getStatusDropdown();
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
		$this->load->vars($data);
		$this->load->view('itstock/layout'); 
    }
    
    public function printReport()
    {
        $data['title'] = "Printing Report";
        $data['main'] = 'itstock/printreport';
        $data['leftside'] = 'content-left';     
        $data['lefttype'] = 'itstock';
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['ittypeList'] = $this->MStatic->getITTypeDropdown();
        $data['modelList'] = $this->MStatic->getModelDropdown();
        $data['locationList'] = $this->MStatic->getLocationDropdown();
        
        $ittype = $this->input->post('ittype');
        $model = $this->input->post('model');
        $branch = $this->input->post('branch');
        $location = $this->input->post('location');
        $submit = $this->input->post('submit');
        
        if($submit)
        {
            $data['ittype'] = $ittype;
            $data['model'] = $model;
            $data['branch'] = $branch;
            $data['location'] = $location; 
            $reports = $this->MITStock->getReportPrinting($ittype,$model,$branch,$location);
            if($submit == "search")
            {
                $data['reports'] = $reports->result();
        		$this->load->vars($data);
        		$this->load->view('itstock/layout'); 
            }
            elseif($submit == "export" && (is_array($reports->result()) && count($reports->result()))){
                report_printing_csv($reports->result(),TRUE,'Report_'.date('dMy').'.csv');   
            }
            else
            {
                $data['reports'] = $reports->result();
        		$this->load->vars($data);
        		$this->load->view('itstock/layout');  
            }   
        }
        else{
    		$this->load->vars($data);
    		$this->load->view('itstock/layout'); 
        } 
    }
       
    public function deleteStock($id=0)
    {
        $this->MITStock->deleteStock($id);
  		$this->session->set_flashdata('message','item deleted');
  		redirect('itstock','refresh');   
    }
    
    public function deleteUsed($id=0, $cat=0)
    {
        $this->MITStock->deleteItem($id);
  		$this->session->set_flashdata('message','Item deleted');
  		redirect('itstock/used','refresh');   
    }
    
    public function deleteBroken($id=0)
    {
        $this->MITStock->deleteBroken($id);
  		$this->session->set_flashdata('message','Item deleted');
  		redirect('itstock/broken','refresh');   
    }
/*--category--*/
    public function catlist()
    {
        $data['title'] = "Manage Category";
		$data['main'] = 'itstock/catlist';
        $data['items'] = $this->MSetting->getITCategoryList();
		$this->load->vars($data);
		$this->load->view('itstock/layout');    
    }
    
    public function catAdd()
    {
        if($this->input->post('ittype'))
        {
            $this->MSetting->addEditITCategory();
	  		$this->session->set_flashdata('message','Category added');
	  		redirect('itstock/catlist','refresh');
        }
        $data['title'] = "Add New Category";
		$data['main'] = 'itstock/catadd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('itstock/catlist');
		$this->load->vars($data);
		$this->load->view('itstock/layout');    
    }
    
    public function catEdit($id=0)
    {
        if($this->input->post('ittype'))
        {
            $this->MSetting->addEditITCategory(id_clean($this->input->post('ittype_id')));
	  		$this->session->set_flashdata('message','Category edited');
	  		redirect('itstock/catlist','refresh');
        }
        $data['title'] = "Update category";
		$data['main'] = 'itstock/catedit';
        $data['items'] = $this->MSetting->getITCategoryById($id);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('itstock/catlist','itstock/deleteitcat/'.$id);
		$this->load->vars($data);
		$this->load->view('itstock/layout');    
    }
    
    public function deleteItcat($id=0)
    {
        $this->MSetting->deleteitcat($id);
       	$this->session->set_flashdata('message','Category deleted');
  		redirect('itstock/catlist','refresh');    
    }
/*---end---*/

/*--model--*/
    public function modellist()
    {
        $data['title'] = "Manage Model";
		$data['main'] = 'itstock/modellist';
        $data['items'] = $this->MSetting->getITModelList();
		$this->load->vars($data);
		$this->load->view('itstock/layout');    
    }
    
    public function modelAdd()
    {
        if($this->input->post('model'))
        {
            $this->MSetting->addEditITModel();
	  		$this->session->set_flashdata('message','Model added');
	  		redirect('itstock/modellist','refresh');
        }
        $data['title'] = "Add New Model";
		$data['main'] = 'itstock/modeladd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('itstock/modellist');
		$this->load->vars($data);
		$this->load->view('itstock/layout');    
    }
    
    public function modelEdit($id=0)
    {
        if($this->input->post('model'))
        {
            $this->MSetting->addEditITModel(id_clean($this->input->post('model_id')));
	  		$this->session->set_flashdata('message','Model edited');
	  		redirect('itstock/modellist','refresh');
        }
        $data['title'] = "Update model";
		$data['main'] = 'itstock/modeledit';
        $data['items'] = $this->MSetting->getITModelById($id);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('itstock/modellist','itstock/deleteitmodel/'.$id);
		$this->load->vars($data);
		$this->load->view('itstock/layout');    
    }
    
    public function deleteitmodel($id=0)
    {
        $this->MSetting->deleteModel($id);
       	$this->session->set_flashdata('message','Model deleted');
  		redirect('itstock/catlist','refresh');    
    }
/*---end---*/
	    
/*---end FA & NA----*/
    
    public function test()
    {
        $this->MITStock->addEditItem(id_clean($this->input->post('stock_id')), id_clean($this->input->post('item_id')));
        //$this->MITStock->addEditStock();
    }
}
